﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHMS.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using WMS.HelperClasses;
using WHMS.EntityClasses;

namespace WMS
{
    public partial class fromGeneralEntityPicker : Form
    {
        public string SelectedItemCode { get; set; }
        public int SelectedItemId { get; set; }
        public string SelectedEntityType { get; set; }
        public int ItemType { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityType">Store or Item</param>
        public fromGeneralEntityPicker(string entityType)
        {
            InitializeComponent();
            SelectedEntityType = entityType;
        }

        public fromGeneralEntityPicker(int itemType)
        {
            InitializeComponent();
            ItemType = itemType;
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            //var fields = new ResultsetFields(4);
            //fields.DefineField(ItemFields.Id, 0, "Id");
            //fields.DefineField(ItemFields.Code, 1, "Code");
            //fields.DefineField(ItemFields.Name, 2, "Name");
            //fields.DefineField(UnitFields.Name, 3, "Unit");

            //SortExpression sorter = null;
            //var filter = new RelationPredicateBucket();
            //filter.Relations.Add(ItemEntity.Relations.ItemTypeEntityUsingTypeId);
            //filter.Relations.Add(ItemEntity.Relations.UnitEntityUsingUnitId);

            //filter.PredicateExpression.Add(ItemFields.TypeId == ItemType);

            //if (SearchByCodeCB.Checked && !string.IsNullOrEmpty(SearchCodeTB.Text))
            //{
            //    var codeTerm = SearchCodeTB.Text.Trim() + "%";
            //    filter.PredicateExpression.Add(ItemFields.Code % codeTerm);

            //    sorter = new SortExpression(ItemFields.Code | SortOperator.Ascending);
            //}

            //if (SearchByNameCB.Checked && !string.IsNullOrEmpty(SearchNameTB.Text))
            //{
            //    var nameTerm = SearchNameTB.Text.Trim() + "%";
            //    filter.PredicateExpression.Add(ItemFields.Name % nameTerm);

            //    sorter = new SortExpression(ItemFields.Name | SortOperator.Ascending);
            //}

            //var dt = DALHelper.GetDynamicList(fields, filter, sorter);
            //dataGridView1.DataSource = dt;

            //if (dt.Rows.Count > 0)
            //{
            //    dataGridView1.Rows[0].Selected = true;
            //}

            //////////////////////
            ////////////////////


            var fields = new ResultsetFields(3);
            
            SortExpression sorter = new SortExpression();
            var filter = new RelationPredicateBucket();

            if (SelectedEntityType == "Item")
            {
                fields.DefineField(ItemFields.Id, 0, "Id");
                fields.DefineField(ItemFields.Code, 1, "Code");
                fields.DefineField(ItemFields.Name, 2, "Name");

                //filter.Relations.Add(ItemEntity.Relations.ItemTypeEntityUsingTypeId);
                //filter.Relations.Add(ItemEntity.Relations.UnitEntityUsingUnitId);
            }
            else if (SelectedEntityType == "Store")
            {

                fields.DefineField(StoreFields.Id, 0, "Id");
                fields.DefineField(StoreFields.Code, 1, "Code");
                fields.DefineField(StoreFields.Name, 2, "Name");

                //filter.Relations.Add(StoreEntity.Relations.ProjectEntityUsingProjectId);
                //filter.Relations.Add(StoreEntity.Relations.StoreShelfBalanceEntityUsingStoreId);
            }


            else if (SelectedEntityType == "Supplier")
            {

                fields.DefineField(SupplierFields.Id, 0, "Id");
                fields.DefineField(SupplierFields.Code, 1, "Code");
                fields.DefineField(SupplierFields.Name, 2, "Name");
            }

            else if (SelectedEntityType == "Contractor")
            {

                fields.DefineField(ContractorFields.Id, 0, "Id");
                fields.DefineField(ContractorFields.Code, 1, "Code");
                fields.DefineField(ContractorFields.Name, 2, "Name");
            }

            if (SearchByCodeCB.Checked)
            {
                if (SelectedEntityType == "Item")
                {
                    sorter = new SortExpression(ItemFields.Code | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Store")
                {
                    sorter = new SortExpression(StoreFields.Code | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Supplier")
                {
                    sorter = new SortExpression(SupplierFields.Code | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Contractor")
                {
                    sorter = new SortExpression(ContractorFields.Code | SortOperator.Ascending);
                }
            }
            else if (SearchByNameCB.Checked)
            {
                if (SelectedEntityType == "Item")
                {
                    sorter = new SortExpression(ItemFields.Name | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Store")
                {
                    sorter = new SortExpression(StoreFields.Name | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Supplier")
                {
                    sorter = new SortExpression(SupplierFields.Name | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Contractor")
                {
                    sorter = new SortExpression(ContractorFields.Name | SortOperator.Ascending);
                }
            }

            if (SearchByCodeCB.Checked && !string.IsNullOrEmpty(SearchCodeTB.Text))
            {
                var codeTerm = SearchCodeTB.Text.Trim() + "%";

                if (SelectedEntityType == "Item")
                {
                    filter.PredicateExpression.Add(ItemFields.Code % codeTerm);


                }
                else if (SelectedEntityType == "Store")
                {
                    filter.PredicateExpression.Add(StoreFields.Code % codeTerm);


                }
                else if (SelectedEntityType == "Supplier")
                {
                    filter.PredicateExpression.Add(SupplierFields.Code % codeTerm);


                }
                else if (SelectedEntityType == "Contractor")
                {
                    filter.PredicateExpression.Add(ContractorFields.Code % codeTerm);


                }
            }

            if (SearchByNameCB.Checked && !string.IsNullOrEmpty(SearchNameTB.Text))
            {
                var nameTerm = SearchNameTB.Text.Trim() + "%";

                if (SelectedEntityType == "Item")
                {
                    filter.PredicateExpression.Add(ItemFields.Name % nameTerm);


                }
                else if (SelectedEntityType == "Store")
                {

                    filter.PredicateExpression.Add(StoreFields.Name % nameTerm);


                }
                else if (SelectedEntityType == "Supplier")
                {

                    filter.PredicateExpression.Add(SupplierFields.Name % nameTerm);


                }
                else if (SelectedEntityType == "Contractor")
                {

                    filter.PredicateExpression.Add(ContractorFields.Name % nameTerm);


                }
            }

            var dt = DALHelper.GetDynamicList(fields, filter, sorter);
            dataGridView1.DataSource = dt;

            if (dt.Rows.Count > 0)
            {
                dataGridView1.Rows[0].Selected = true;
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var name = (string)dataGridView1.SelectedRows[0].Cells[2].Value;
                var code = (string)dataGridView1.SelectedRows[0].Cells[1].Value;
                //var unit = (string)dataGridView1.SelectedRows[0].Cells[3].Value;

                SelectedItemCode = code ;
                SelectedItemId = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
            }

            this.DialogResult = DialogResult.OK;

            this.Close();
        }

        private void SearchByCodeCB_CheckedChanged(object sender, EventArgs e)
        {
            SearchCodeTB.Enabled = SearchByCodeCB.Checked;
        }

        private void SearchByNameCB_CheckedChanged(object sender, EventArgs e)
        {
            SearchNameTB.Enabled = SearchByNameCB.Checked;
        }
    }
}
